<?php
/**
*	转pdf的
*/
namespace Rainrock\Framework\Chajian;


use Rainrock\Framework\kernel\core\Chajian;
use Rainrock\Framework\kernel\base\Base;
use Rainrock\Framework\kernel\base\Jiami;




class pdfChajian extends Chajian{
	
	/**
	*	转pdf，需要完整目录
	*/
	public function topdf($filepath, $filename='')
	{
		$barr		= $this->istopdf();
		if(!$barr['success'])return $barr;
		
		$path	= $barr['data']['path'];
		$type	= $barr['data']['type'];
		if(!file_exists($filepath))return $this->returnerror('file noexists');

		$fileext 	= $this->c('upfile')->getexts($filepath);
		$pdfpath	= str_replace('.'.$fileext.'','.pdf',$filepath);
		
		if($type=='upload'){
			$curl   = $this->c('curl')->setTimeout(60*5);
			$url 	= $path.'?m=topdf&a=index';
			if(!$filename)$filename = substr($filepath,strrpos($filepath,'/')+1);
			$result = $curl->postcurl($url, array('file' => new \CURLFile($filepath, '', $filename)), array(), 1);
			$tarr	= $curl->result($result);
			if(!$tarr['success'])return $tarr;
			$data 	= $tarr['data'];
			$pdfurl = $data['pdfpath']; //转好的URL地址，下载到本地
			@file_put_contents($pdfpath, file_get_contents($pdfurl));
			if(!file_exists($pdfpath)){
				$sarr = $this->returnsuccess('save error');
			}else{
				$sarr = $this->returnsuccess(array(
					'pdfpath' => $pdfpath
				));
			}
			//删除远程的文件
			$curl->getcurl($path.'?m=topdf&a=del&fileext='.$data['fileext'].'&filepath='.Jiami::base64encode($data['filepath']).'');
			return $sarr;
		}
		
		$outdir		= substr($filepath,0, strripos($filepath,'/'));
		$cmd		= '"'.$path.'" --headless --invisible --convert-to pdf:writer_pdf_Export "'.$filepath.'" --outdir "'.$outdir.'"';
		$output 		= null;
		$result_code 	= null;
		exec($cmd, $output, $result_code);
		if($result_code == 0 && file_exists($pdfpath))return $this->returnsuccess(array(
			'pdfpath' => $pdfpath
		));
		return $this->returnerror('error('.$result_code.')');
	}
	
	public function istopdf()
	{
		$path 		= Base::getConfig('libreoffice_path');
		if($this->isempt($path)){
			$topdf_url = Base::getConfig('topdf_url');
			if(!$topdf_url){
				return $this->returnerror('no config topdf_url');
			}else{
				return $this->returnsuccess(array(
					'path' => $topdf_url,
					'type' => 'upload'
				));
			}
		}
		if(!$path)return $this->returnerror('no config libreoffice_path');
		if(!function_exists('exec'))return $this->returnerror('exec noexists');
		if($this->contain(PHP_OS,'WIN')){
			$soffice	= ''.$path.'/program/soffice.exe';
		}else{
			$soffice	= $path;
		}
		$soffice	= str_replace('\\','/', $soffice);
		if(!file_exists($soffice))return $this->returnerror('path noexists');
		
		return $this->returnsuccess(array(
			'path' => $soffice,
			'type' => 'libreoffice'
		));
	}
	
	/**
	*	上传转pdf
	*/
	public function upload()
	{
		$barr		= $this->istopdf();
		if(!$barr['success'])return $barr;
		$obj  		= $this->c('upfile')->initUpfile('doc,docx,ppt,pptx,xls,xlsx', ''.UPDIR.'/topdf');
		$obj->setRootsave(true);
		$barr 		= $obj->up();
		if(!is_array($barr))return $this->returnerror($barr);
		$filepath 	= $barr['allfilename']; //这个没有跟目录的路径
		$fileext 	= $obj->getexts($filepath);
		$pdfpath	= str_replace('.'.$fileext.'','.pdf', $filepath);
		
		$tarr 		= $this->topdf(ROOT_PATH.'/'.$filepath);
		if(!$tarr['success'])return $tarr;
		
		$data['pdfpath']  = URLN.$pdfpath;
		$data['fileext']  = $fileext;
		$data['filepath'] = $filepath;
		
		return $this->returnsuccess($data);
	}
}
	